import 'package:flutter/material.dart';
import 'package:flutter_ability/custom_ui.dart';

/// 组件上用于提示的角标
/// 默认是带圆角的矩形
class YJBadge extends StatefulWidget {
  const YJBadge({
    Key? key,
    this.textStyle,
    this.borderShape,
    this.shape = YJBadgeShape.standard,
    this.color = YJColors.WARNING,
    this.textColor = YJColors.WHITE,
    this.size,
    this.border,
    this.text,
    this.child,
  }) : super(key: key);

  /// 边框
  final BorderSide? border;

  /// 边框形状
  final ShapeBorder? borderShape;

  /// 角标形状(YJBadgeShape.circle)circle、pills、square、默认standard
  final YJBadgeShape shape;

  /// 角标背景颜色
  final Color color;

  /// 角标尺寸 自定义尺寸（double）
  final double? size;

  /// 子组件 如text不为null此组件失效
  final Widget? child;

  /// 角标文本
  final String? text;

  /// 角标文本样式
  final TextStyle? textStyle;

  /// 角标文本颜色
  final Color textColor;

  @override
  _YJBadgeState createState() => _YJBadgeState();
}

class _YJBadgeState extends State<YJBadge> {
  late Color color;
  late Color textColor;
  Widget? child;
  YJBadgeShape? counterShape;
  double? size;
  double? height;
  double? width;
  double? fontSize;

  @override
  void initState() {
    color = widget.color;
    textColor = widget.textColor;
    String? mText = widget.text;
    if (widget.text != null) {
      mText = mText!.length <= 3 ? mText : mText.substring(0, 3);
    }
    child = widget.text != null ? Text(mText ?? '') : widget.child;
    counterShape = widget.shape;
    size = widget.size;
    super.initState();
  }

  @override
  void didUpdateWidget(YJBadge oldWidget) {
    color = widget.color;
    textColor = widget.textColor;
    child = widget.text != null
        ? Text(widget.text ?? '', maxLines: 1)
        : widget.child;
    counterShape = widget.shape;
    size = widget.size;
    super.didUpdateWidget(oldWidget);
  }

  @override
  Widget build(BuildContext context) {
    final BorderSide shapeBorder = widget.border != null
        ? widget.border!
        : BorderSide(
            color: color,
            width: 0,
          );

    ShapeBorder shape;

    if (counterShape == YJBadgeShape.pills) {
      shape = RoundedRectangleBorder(
        borderRadius: BorderRadius.circular(50),
        side: shapeBorder,
      );
    } else if (counterShape == YJBadgeShape.square) {
      shape = RoundedRectangleBorder(
        borderRadius: BorderRadius.circular(0),
        side: shapeBorder,
      );
    } else if (counterShape == YJBadgeShape.standard) {
      shape = RoundedRectangleBorder(
        borderRadius: BorderRadius.circular(5),
        side: shapeBorder,
      );
    } else if (counterShape == YJBadgeShape.circle) {
      shape = RoundedRectangleBorder(
        borderRadius: BorderRadius.circular(100),
        side: shapeBorder,
      );
    } else {
      shape = RoundedRectangleBorder(
        borderRadius: BorderRadius.circular(5),
        side: shapeBorder,
      );
    }

    /// 角标尺寸
    if (size == null) {
      if (widget.text == "") {
        height = YJSize.dp(8);
        width = YJSize.dp(8);
        fontSize = YJSize.sp(12);
      } else if (widget.text?.length == 1) {
        height = YJSize.dp(16);
        width = YJSize.dp(16);
        fontSize = YJSize.sp(12);
      } else {
        height = YJSize.dp(16);
        width = YJSize.dp(30);
        fontSize = YJSize.sp(12);
      }
    } else {
      height = size! * 0.6;
      width = size! * 0.79;
      fontSize = size! * 0.37;
    }

    if (counterShape == YJBadgeShape.circle) {
      width = height;
    }

    return Container(
      height: height,
      width: width,
      child: Material(
        textStyle: TextStyle(
          color: textColor,
          fontSize: fontSize,
        ),
        shape: widget.borderShape ?? shape,
        color: color,
        type: MaterialType.button,
        child: Container(
          alignment: Alignment.center,
          child: Center(
            widthFactor: 1,
            heightFactor: 1,
            child: child,
          ),
        ),
      ),
    );
  }
}
